package com.joesem.sale.client.logic.event

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.addTextChangedListener
import com.bumptech.glide.Glide
import com.goodPermission.expansion.launchCamera
import com.goodPermission.expansion.launchPic
import com.google.gson.Gson
import com.jiaxing.sale.client.databinding.ActivityEventRetureBinding
import com.jiaxing.sale.client.databinding.ItemImgBinding
import com.jiaxing.sale.client.databinding.LayoutToolbarBinding
import com.joesem.sale.client.base.*
import com.joesem.sale.client.network.data.EventDetailsMode
import com.joesem.sale.client.network.data.UploadImgMode
import com.joesem.sale.client.utils.activityResult.launcherResult
import com.joesem.sale.client.utils.click
import com.joesem.sale.client.widget.dialog.picDialog


class EventReturnActivity : BaseActivity<ActivityEventRetureBinding>() {

    private val viewModel by createViewModel<EventViewModel>()

    private val orderValue = HashMap<String, Any?>()

    override fun LayoutToolbarBinding.toolbar(savedInstanceState: Bundle?) {
        visible()
        toolbarTheme()
        toolbarTitle.text = "事件回执"
    }

    override fun ActivityEventRetureBinding.initView(savedInstanceState: Bundle?) {

    }

    override fun ActivityEventRetureBinding.initListener() {
        etResultText.addTextChangedListener {
            orderValue["description"] = it.toString()
        }
        btnImgAdd.click {
            picDialog(this@EventReturnActivity, pic = {
                launchPic("order" to "event_${System.currentTimeMillis()}.jpg"){
                    it.file?.let { it1 -> viewModel.uploadImg(it1) }
                }
            }, camera = {
                launchCamera("order" to "event_${System.currentTimeMillis()}.jpg"){
                    it.file?.let { it1 -> viewModel.uploadImg(it1) }
                }
            })
        }
        btnConfirm.click {
            if (orderValue["description"] == null || orderValue["description"]?.toString()
                    .isNullOrEmpty()
            ) {
                toast("请输入处理结果~")
                return@click
            }
            if (selectedImage.isEmpty()) {
                toast("请添加现场照片~")
                return@click
            }
            viewModel.confirmReturn(intent.getIntExtra("id", -1), orderValue)
        }
    }

    override fun ActivityEventRetureBinding.initIntent(intent: Any?) {
        when (intent) {
            is EventViewModel.ImgUploadIntent -> {
                addImageItem(intent.mode)
            }
            is EventViewModel.EventDetailsIntent -> {
                intent.data?.let {
                    setResult(Activity.RESULT_OK, Intent().apply {
                        putExtra("returnResult", Gson().toJson(it))
                    })
                    finish()
                }
            }
        }
    }

    private val selectedImage = arrayListOf<UploadImgMode>()
    private fun addImageItem(file: UploadImgMode) {
        selectedImage.add(file)
        orderValue["images"] = selectedImage.map { it.path }
        if (bind.llImgContent.childCount >= 2) {
            bind.btnImgAdd.gone()
        }
        val itemBind = ItemImgBinding.inflate(LayoutInflater.from(this))
        itemBind.btnImgDelete.click {
            selectedImage.remove(file)
            orderValue["images"] = selectedImage.map { it.path }
            bind.llImgContent.removeView(itemBind.root)
            bind.btnImgAdd.visible()
        }
        Glide.with(this).load(file.url).into(itemBind.btnImg)
        bind.llImgContent.addView(itemBind.root, 0)
    }
}


fun optionEventReturn(activity: AppCompatActivity, id: Int?, result: (EventDetailsMode) -> Unit) {
    activity.launcherResult<EventReturnActivity>("id" to id) { _: Int, data: Intent? ->
        data?.getStringExtra("returnResult")?.let {
            Gson().fromJson(it, EventDetailsMode::class.java)?.let(result)
        }
    }
}